package linked.base;

// ... existing code ...
public class BaseDemo1 {

    static ListNode deleteNode(ListNode head, int i) {
        ListNode firNode = new ListNode(0, head);
        ListNode cur = firNode;
        for (int i1 = 0; i1 < i; i1++) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return firNode.next;
    }

    static ListNode insertNode(ListNode head, int i, ListNode newNode) {
        ListNode firNode = new ListNode(0, head);
        ListNode cur = firNode;
        for (int i1 = 0; i1 < i; i1++) {
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        return firNode.next;
    }

    static int sumNode(ListNode head) {
        int len = 0;
        while (head != null) {
            len++;
            head = head.next;
        }
        return len;
    }

    // 优化打印方法，添加箭头和换行，更清晰地显示链表结构
    static void printList(ListNode head) {
        if (head == null) {
            System.out.println("Empty list");
            return;
        }

        while (head != null) {
            System.out.print(head.val);
            if (head.next != null) {
                System.out.print(" -> ");
            }
            head = head.next;
        }
        System.out.println();
    }

    // 添加一个方法用于创建链表，使测试代码更简洁
    static ListNode createList(int... values) {
        if (values.length == 0) return null;
        ListNode firNode = new ListNode(0);
        ListNode cur = firNode;
        for (int v : values) {
            ListNode newNode = new ListNode(v);
            cur.next = newNode;
            cur = cur.next;
        }
        return firNode.next;
    }

    public static void main(String[] args) {
        System.out.println("=== 链表操作测试 ===\n");

        // 测试创建和统计节点
        int[] values1 = {1, 2, 3, 4, 5};
        ListNode list1 = createList(1, 2, 3, 4, 5);
        int count = sumNode(list1);
        System.out.println("链表: " + arrayToString(values1) + " => 节点数: " + count + " (预期: 5) " + (count == 5 ? "✓" : "✗"));

        // 测试删除操作
        ListNode list2 = createList(1, 2, 3, 4, 5);
        ListNode deletedList = deleteNode(list2, 0); // 删除索引0的节点
        String result2 = listToString(deletedList);
        String expected2 = "[2,3,4,5]";
        System.out.println("链表: [1,2,3,4,5], 删除索引: 0 => 结果: " + result2 + " (预期: " + expected2 + ") " +
                (result2.equals(expected2) ? "✓" : "✗"));

        // 测试在中间插入节点
        ListNode list3 = createList(1, 2, 3, 4, 5);
        ListNode insertedList = insertNode(list3, 2, new ListNode(0)); // 在索引2前插入0
        String result3 = listToString(insertedList);
        String expected3 = "[1,2,0,3,4,5]";
        System.out.println("链表: [1,2,3,4,5], 在索引2前插入: 0 => 结果: " + result3 + " (预期: " + expected3 + ") " +
                (result3.equals(expected3) ? "✓" : "✗"));

        // 测试删除尾节点
        ListNode list4 = createList(1, 2, 3);
        ListNode tailDeleted = deleteNode(list4, 2); // 删除索引2的节点
        String result4 = listToString(tailDeleted);
        String expected4 = "[1,2]";
        System.out.println("链表: [1,2,3], 删除索引: 2 => 结果: " + result4 + " (预期: " + expected4 + ") " +
                (result4.equals(expected4) ? "✓" : "✗"));

        // 测试删除唯一节点
        ListNode list5 = createList(1);
        ListNode lastDeleted = deleteNode(list5, 0); // 删除唯一节点
        String result5 = listToString(lastDeleted);
        String expected5 = "[]";
        System.out.println("链表: [1], 删除索引: 0 => 结果: " + result5 + " (预期: " + expected5 + ") " +
                (result5.equals(expected5) ? "✓" : "✗"));

        // 测试在开头插入节点
        ListNode list6 = createList(2, 3, 4);
        ListNode headInsert = insertNode(list6, 0, new ListNode(1)); // 在开头插入1
        String result6 = listToString(headInsert);
        String expected6 = "[1,2,3,4]";
        System.out.println("链表: [2,3,4], 在索引0前插入: 1 => 结果: " + result6 + " (预期: " + expected6 + ") " +
                (result6.equals(expected6) ? "✓" : "✗"));
    }


    // 辅助方法：将数组转换为字符串
    static String arrayToString(int[] arr) {
        if (arr == null || arr.length == 0) return "[]";
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i < arr.length - 1) sb.append(",");
        }
        sb.append("]");
        return sb.toString();
    }

    // 辅助方法：将链表转换为字符串
    static String listToString(ListNode head) {
        if (head == null) return "[]";
        StringBuilder sb = new StringBuilder("[");
        while (head != null) {
            sb.append(head.val);
            if (head.next != null) {
                sb.append(",");
            }
            head = head.next;
        }
        sb.append("]");
        return sb.toString();
    }
// ... existing code ...

}
